/* Copyright 2011 Brian Cairns
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.wsnetwork.reforger;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Item {
    private static final Pattern NAME_PATTERN = Pattern.compile("<name><\\!\\[CDATA\\[(.*)\\]\\]></name>");
    
    private final String slotName;
    private final String name;
    private final StatVector baseStats;
    private final boolean hasRandomEnchant;

    public Item(int id, URLRetriever urlRetriever) {
        URL url;
        try {
            url = new URL("http://www.wowhead.com/item=" + id + "&xml");
            String data = urlRetriever.getUrlContents(url);
            Matcher nameMatcher = NAME_PATTERN.matcher(data);
            nameMatcher.find();
            name = nameMatcher.group(1);
            baseStats = new StatVector(data);
            hasRandomEnchant = data.contains("Random enchantment");
            slotName = "";
        } catch (MalformedURLException e) {
            throw new RuntimeException("Unable to read statistics for item ID: " + id, e);
        } catch (IOException e) {
            throw new RuntimeException("Unable to read statistics for item ID: " + id, e);
        }
    }
    
    public Item(String sn,String n, StatVector stats){
    	slotName = sn;
    	name = n;
    	baseStats = stats;
    	hasRandomEnchant = false;
    }

    public StatVector getStats() {
        return baseStats;
    }

    public String getName() {
        return name;
    }
    
    public String getSlotName() {
    	return slotName;
    }

    public Collection<StatVector> getPossibleReforgings(Map<Stat, Set<Stat>> possibleMappings) {
        List<StatVector> result = new ArrayList<StatVector>();

        for (Stat from : Stat.possibleStats) {
            for (Stat to : Stat.possibleStats) {
                if (baseStats.getStat(to) == 0 && baseStats.getStat(from) != 0) {
                    if (possibleMappings.containsKey(from)) {
                        if (possibleMappings.get(from).contains(to)) {
                            int delta = (int)Math.floor(baseStats.getStat(from) * 0.4);
                            StatVector deltaVector = new StatVector(from, to, delta);
                            result.add(deltaVector);
                        }
                    }
                }
            }
        }

        return result;
    }

    @Override public String toString() {
        return "[\"" + name + "\" Stats=" + baseStats + "]";
    }

    public boolean hasRandomEnchant() {
        return hasRandomEnchant;
    }
}